package landun.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import landun.pojo.BizClaimVoucher;
import landun.pojo.BizClaimVoucherDetail;
import landun.pojo.Page;
import landun.pojo.SysEmployee;
import landun.service.BizClaimVoucherDetailService;
import landun.service.BizClaimVoucherService;
import landun.service.EmpService;

import java.sql.Time;
import java.util.*;

public class VoucherAction extends ActionSupport {
    private Integer pageNo;
    //分页对象
    private Page pageSupport ;
    //报销单service
    private BizClaimVoucherService claimVoucherService;
    //报销单对象
    private BizClaimVoucher claimVoucher;
    //报销单集合
    private List<BizClaimVoucher> claimVoucherList ;
    //报销单状态map
    private Map<String,String> statusMap = new HashMap<>();
    //报销详单
    private List<BizClaimVoucherDetail> detailList;
    //报销详单service
    private BizClaimVoucherDetailService bizClaimVoucherService;

    private EmpService empService;
    private Date startDate;

    private Date endDate;

    Map<String, Object> session = ActionContext.getContext().getSession();

    private SysEmployee employee = (SysEmployee)session.get("user");
    //查询报销单
    public String searchClaimVoucher(){
        if(pageSupport==null){
            pageSupport = new Page();
            pageSupport.setCurrentPageNo(1);
            if(claimVoucher==null){
                claimVoucher=new BizClaimVoucher();
            }
            pageSupport.setTotalRecordCount(claimVoucherService.totalCount(claimVoucher.getStatus(),startDate,endDate,employee));
        }
        if(pageNo!=null){
            pageSupport.setCurrentPageNo(pageNo);
        }
        List<Object[]> statusList= claimVoucherService.findStatusMap();

        for (int i = 0; i < statusList.size(); i++) {
            Object objects = statusList.get(i);
            statusMap.put(objects.toString(),objects.toString());
        }
        claimVoucherList=claimVoucherService.findClaimVoucher(claimVoucher.getStatus(),startDate,endDate,employee,pageSupport);
        return "claim_voucher_list";
    }
    //跳转报销单add.jsp
    public String toAdd(){
        return "toAdd";
    }
    //保存报销单
    public String saveClaimVoucher(){
        claimVoucher.setCreateTime(new Time(System.currentTimeMillis()));
        claimVoucher.setCreator(employee);
        if (claimVoucher.getStatus().equals("已提交")) {
            claimVoucher.setNextDeal(empService.findByPosition(2,employee.getDepartmentId()));
        } else {
            claimVoucher.setNextDeal(claimVoucher.getCreator());
        }
        //添加报销单
        for (BizClaimVoucherDetail c:detailList) {
            c.setMainId(claimVoucher);
        }
        Set<BizClaimVoucherDetail> bizClaimVoucherDetailSet = new HashSet<>(detailList);
        claimVoucher.setDetailList(bizClaimVoucherDetailSet);
        claimVoucherService.save(claimVoucher);

        //添加报销单明细
        return "redirectList";
    }
    //跳转修改报销单
    public String toUpdate(){

        return "update";
    }

    public String getClaimVoucherById(){
        claimVoucher= claimVoucherService.findVoucherById(claimVoucher.getId());
        return "view";
    }
    //删除
    public String deleteClaimVoucherById(){

        return "delete";
    }
    //审查报销单
    public String toCheck(){
        claimVoucher= claimVoucherService.findVoucherById(claimVoucher.getId());
        return "check";
    }

    public List<BizClaimVoucherDetail> getDetailList() {
        return detailList;
    }

    public void setDetailList(List<BizClaimVoucherDetail> detailList) {
        this.detailList = detailList;
    }

    public BizClaimVoucherDetailService getBizClaimVoucherService() {
        return bizClaimVoucherService;
    }

    public void setBizClaimVoucherService(BizClaimVoucherDetailService bizClaimVoucherService) {
        this.bizClaimVoucherService = bizClaimVoucherService;
    }

    public EmpService getEmpService() {
        return empService;
    }

    public void setEmpService(EmpService empService) {
        this.empService = empService;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public BizClaimVoucherService getClaimVoucherService() {
        return claimVoucherService;
    }

    public void setClaimVoucherService(BizClaimVoucherService claimVoucherService) {
        this.claimVoucherService = claimVoucherService;
    }


    public BizClaimVoucher getClaimVoucher() {
        return claimVoucher;
    }

    public void setClaimVoucher(BizClaimVoucher claimVoucher) {
        this.claimVoucher = claimVoucher;
    }

    public List<BizClaimVoucher> getClaimVoucherList() {
        return claimVoucherList;
    }

    public void setClaimVoucherList(List<BizClaimVoucher> claimVoucherList) {
        this.claimVoucherList = claimVoucherList;
    }

    public Map<String, String> getStatusMap() {
        return statusMap;
    }

    public void setStatusMap(Map<String, String> statusMap) {
        this.statusMap = statusMap;
    }

    public Page getPageSupport() {
        return pageSupport;
    }

    public void setPageSupport(Page pageSupport) {
        this.pageSupport = pageSupport;
    }
}
